package com.jyh.util;
/**
 * 
 */

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

public class JYHNoUtils {
	private static final char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V' };
	private static String ip = null;
	private static AtomicLong number = new AtomicLong(0);
	private static long recordIpTime = 0;
	private static final String maxNumStr = "VVVV";
	private static final int maxLength = 4;
	private static final long maxNum = UnCompressNumber(maxNumStr);
	private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");

//	public static void main(String[] args) {
//		Map<String,String> map = new HashMap<String,String>();
//		for(int i=0;i<10;i++){
//			String no = getJYHNo();
//			map.put(no, no);
//			System.out.println(no);
//		}
//		System.out.println("----------------------------" + map.size());
//	}

	/**
	 * 获取一个订单编号
	 * 
	 * @return String
	 */
	public static String getJYHNo() {
		return getJYHNo("P");
	}

	/**
	 * 获取一个编号
	 * 
	 * @return String
	 */
	public static String getJYHNo(String prefix) {
		synchronized (JYHNoUtils.class) {
			checkIp();
			String time = getTime();
			long num1 = number.getAndIncrement();
			if (num1 > maxNum) {
				num1 = 0;
				number.set(0);
			}
			String num = CompressNumber(num1);

			while (num.length() < maxLength) {
				num = "0" + num;
			}
			if (num.length() > maxLength) {
				num = num.substring(num.length() - maxLength, num.length());
			}
			return prefix + time + ip + num;
		}
	}

	/**
	 * @return String
	 */
	private static String getTime() {
		String time = formatter.format(new Date());
		return time;
	}

	private static void checkIp() {
		if (ip == null) {
			getIp();
		} else {
			if (System.currentTimeMillis() - recordIpTime < 1000 * 60) {
				return;
			}
			getIp();
		}
	}

	private static void getIp() {
		try {
			ip = ServiceIpUtils.getLocalIP();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ips[] = ip.split("\\.");
		ip = ips[ips.length - 1];
		while (ip.length() < 3) {
			ip = "0" + ip;
		}
		if (ip.length() > 3) {
			ip = ip.substring(ip.length() - 3, ip.length());
		}
		recordIpTime = System.currentTimeMillis();
	}

	/**
	 * 
	 * @param number
	 * @param shift
	 * @return
	 */
	private static String CompressNumber(long number) {
		char[] buf = new char[32];
		int charPos = 32;
		int radix = 1 << 5;
		long mask = radix - 1;
		do {
			buf[--charPos] = digits[(int) (number & mask)];
			number >>>= 5;
		} while (number != 0);
		String num = new String(buf, charPos, (32 - charPos));
		while (num.length() < maxLength) {
			num = "0" + num;
		}
		return num;
	}

	/**
	 * @param decompStr
	 * @return
	 */
	private static long UnCompressNumber(String decompStr) {
		long result = 0;
		for (int i = decompStr.length() - 1; i >= 0; i--) {
			for (int j = 0; j < digits.length; j++) {
				if (decompStr.charAt(i) == digits[j]) {
					result += ((long) j) << 5 * (decompStr.length() - 1 - i);
				}
			}
		}
		return result;
	}
}
